
//
//  ShouCangViewController.m
//  ShangChengMoBan
//
//  Created by yaoqi yaoqi on 16/10/12.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "ShouCangViewController.h"
#import "TuiStyleTableViewCell.h"
#import "ShouCangModel.h"
#import "SCHomeViewController.h"
@interface ShouCangViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>

@end

@implementation ShouCangViewController
{
 RWScrollView * _table;
    int _page;
    NSMutableArray * _arr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 0;
    [self drawNavWithTitle:@"我的订单" WithColor:[UIColor colorWithHue:0.00 saturation:0.00 brightness:1.00 alpha:1.00]];
    [self drawBackButton];
    _table = [[RWScrollView alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, KScreenHeight-64)];
    [_table addHeaderWithTarget:self action:@selector(headRefresh)];
    [_table headerBeginRefreshing];
    [_table addFooterWithTarget:self action:@selector(footerRefresh)];
    _table.delegate = self;
    _table.dataSource = self;
    [_table registerClass:[TuiStyleTableViewCell class ] forCellReuseIdentifier:@"cell"];
    _table.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:_table];
}
-(void)headRefresh{
    _page = 1;
    [self requestDataWith:1];
}
-(void)footerRefresh{
    _page ++;
    [self requestDataWith:0];
}
-(void)requestDataWith:(int)first{
    NSMutableDictionary * dic =[ [NSMutableDictionary alloc]init];
    [dic setObject:@"10" forKey:@"pageSize"];
    [dic setObject:[NSString stringWithFormat:@"%d",_page] forKey:@"pageNum"];
    [dic setObject:[self defaultLoadUserName] forKey:@"userName"];
    [NetWorking NEWjieKouQingQiu:SC_HuoQu_collect ANDIP:IP And:dic anBlock:^(NSDictionary *dic, NSError *error) {
        [_table headerEndRefreshing];
        [_table footerEndRefreshing];
        if (!error) {
            if ([dic[Code] intValue] == 200) {
                  if (first == 1) {
                    _arr = [[NSMutableArray alloc]init];
                    }
                NSArray * array = dic[Response];
               [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                   ShouCangModel * model = [ShouCangModel objectFromDictionary:obj];
                   [_arr addObject:model];
               }];
                [_table reloadData];
            }
        }
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShouCangModel * model = _arr[indexPath.row];
    TuiStyleTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.title.text =model.goodsMapper.name;
    cell.shuXing.text = [NSString stringWithFormat:@"¥ %@",model.goodsMapper.originalPrice];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.goodsMapper.coverImage]];
    cell.shuXing.textColor = [UIColor redColor];
    [cell.remove setTitle:@"删除收藏" forState:UIControlStateNormal];
    cell.remove.tag = indexPath.row;
    [cell.remove addTarget:self action:@selector(remove:) forControlEvents:UIControlEventTouchUpInside];
    cell.remove.layer.cornerRadius=3;
    cell.remove.layer.borderColor = [UIColor lightGrayColor].CGColor;
    cell.remove.layer.borderWidth=1;
    return  cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
      ShouCangModel * model = _arr[indexPath.row];
    SCHomeViewController *productView = [[SCHomeViewController alloc]init];
    productView.FL_chanPin = model.goodsMapper;
    [self.navigationController pushViewController:productView animated:YES];
}
-(void)remove:(UIButton *)button{
    int tag =(int)button.tag;
    [TCUtils showAlertViewWithTitle:@"提醒" message:@"你确定要取消订单吗?" delegate:self cancel:@"取消" other:@"确定" tag:tag];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
     ShouCangModel * model = _arr[alertView.tag];
    if (buttonIndex ==1) {
            NSMutableDictionary * dic =[ [NSMutableDictionary alloc]init];
            [dic setObject:model.recId forKey:@"recId"];//收藏ID
            [dic setObject:[self defaultLoadUserName] forKey:@"userName"];
            [NetWorking NEWjieKouQingQiu:SC_remove ANDIP:IP And:dic anBlock:^(NSDictionary *dic, NSError *error) {
                if (!error) {
                    if ([dic[Code] intValue] == 200) {
                        [[iToast makeText:@"  成功删除收藏!  "] show];
                        [_table headerBeginRefreshing];
                    }
                }
            }];
        }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
